package com.ruoyi.lab.service.impl;
import java.util.List;
import com.ruoyi.lab.domain.JacTestOutline;
import com.ruoyi.lab.mapper.JacTestOutlineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lab.mapper.JacLabItemcodeMapper;
import com.ruoyi.lab.domain.JacLabItemcode;
import com.ruoyi.lab.service.IJacLabItemcodeService;
import com.ruoyi.common.core.text.Convert;

/**
 * itemcodeService业务层处理
 * @author sxx
 * @date 2021-05-24
 */

@Service
public class JacLabItemcodeServiceImpl implements IJacLabItemcodeService 
{
    @Autowired
    private JacLabItemcodeMapper jacLabItemcodeMapper;
    @Autowired
    private JacTestOutlineMapper jacTestOutlineMapper;

    /**
     * 查询itemcode
     * @param id itemcodeID
     * @return itemcode
     */

    @Override
    public JacLabItemcode selectJacLabItemcodeById(Long id)
    {
        return jacLabItemcodeMapper.selectJacLabItemcodeById(id);
    }

    /**
     * 查询itemcode列表
     * 通过时间节点去查询
     * @param jacLabItemcode itemcode
     * @return itemcode
     */

    @Override
    public List<JacLabItemcode> selectJacLabItemcodeList(JacLabItemcode jacLabItemcode)
    {
        return jacLabItemcodeMapper.selectJacLabItemcodeList(jacLabItemcode);
    }


    /**
     * 新增itemcode
     * @param jacLabItemcode itemcode
     * @return 结果
     */

    @Override
    public int insertJacLabItemcode(JacLabItemcode jacLabItemcode)
    {
        return jacLabItemcodeMapper.insertJacLabItemcode(jacLabItemcode);
    }


    /**
     * 修改itemcode
     * 若与大纲有关联则同时更新大纲处的itemcode
     */


    @Override
    public int updateJacLabItemcode(JacLabItemcode jacLabItemcode)
    {
        if(null!=jacLabItemcode.getItemcode()) {
        List<JacTestOutline> jacTestOutlineList = jacTestOutlineMapper.selectJacTestOutlineByMyItemCode(jacLabItemcode.getPreitemcode());
        for (JacTestOutline jacTestOutline:jacTestOutlineList) {
            jacTestOutline.setItemcode(jacLabItemcode.getItemcode());
            jacTestOutline.setProject(jacLabItemcode.getItemname());
            jacTestOutlineMapper.updateJacTestOutline(jacTestOutline);
        }
     }
        return jacLabItemcodeMapper.updateJacLabItemcode(jacLabItemcode);
    }

    /**
     * 删除itemcode
     * 若与试验大纲有关联则不允许删除
     */


    @Override
    public String deleteJacLabItemcodeByIds(String ids)
    {  String result="1";
        Long[] itemcodeids=Convert.toLongArray(ids);
        String[] a = Convert.toStrArray(result);
        for (Long id:itemcodeids) {
            JacLabItemcode jacLabItemcode=jacLabItemcodeMapper.selectJacLabItemcodeById(id);
            List<JacTestOutline>  jacTestOutlineList=jacTestOutlineMapper.selectJacTestOutlineByMyItemCode(jacLabItemcode.getItemcode());
            if(jacTestOutlineList.size()!=0){
                result="有关联的试验大纲:"+jacTestOutlineList.get(0).getItemcode()+"不允许删除！";
                return result;
            }
        }
         jacLabItemcodeMapper.deleteJacLabItemcodeByIds(Convert.toStrArray(ids));
        return result;
    }

    /**
     * 删除itemcode信息
     * @param id itemcodeID
     * @return 结果
     */


    public int deleteJacLabItemcodeById(Long id)
    {
        return jacLabItemcodeMapper.deleteJacLabItemcodeById(id);
    }
}
